<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Member extends CI_Controller {

	function __construct(){
		parent::__construct();
		authRedirect('/admin/info');
		$this->load->model('member_model');
		$this->load->model('production_model');
	}
	public function index()
	{
		$data['error'] = $this->session->flashdata('error');
		$data['success'] = $this->session->flashdata('success');
		
		$start = $this->uri->segment(4,0);
		$data['m'] = $this->member_model->get_member($start,100);
		$data['num'] = $this->member_model->get_member_num();
		//pagenation
		$this->load->library('pagination');
		$config['base_url'] = '/admin/member/index/';
		$config['total_rows'] = $data['num'];
		$config['per_page'] = 100; 
		$config['full_tag_open'] = '<p class="page">';
		$config['full_tag_close'] = '</p>';
		$config['uri_segment'] = 4;
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['cur_tag_open'] = '<b>';
		$config['cur_tag_close'] = '</b>';
		$config['next_link'] = '下一页';
		$config['prev_link'] = '上一页';
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();

		$this->load->view('admin/member/list.tpl',$data);
	}

	public function action(){
		
		$data['error'] = $this->session->flashdata('error');
		$data['success'] = $this->session->flashdata('success');
		
		$type = $this->uri->segment(4);
		$types = array('zf','ps','ss','cj');
		if( !in_array($type,$types)){show_404();}
		
		$pid = (int)$this->uri->segment(5);
		if(!$pid) show_error('错误的操作');

		$p = $this->production_model->get_production_byid($pid);
		if(!$p) show_error('该商品可能已经被删除');
		$data['p'] = $p;


		$this->load->library('form_validation');
		$this->form_validation->set_rules('content', '内容', 'trim|required');
		if ($this->form_validation->run()) {
			$r = $this->rese_model->action($pid,$type);
			if($r['status']){
				$this->session->set_flashdata('success','添加成功！');
			}else{
				$this->session->set_flashdata('error',$r['msg']);
			}
			redirect(current_url());
		}
		switch($type){
			case 'zf':$data['title'] = '支付方式';break;
			case 'ps':$data['title'] = '配送方式';break;
			case 'ss':$data['title'] = '售后服务';break;
			case 'cj':$data['title'] = '常见问题';break;
		}
		$data['info'] = '';
		$data['action'] = '/admin/rese/action/'.$type.'/'.$pid;
		$data['button'] = '添加'.$data['title'];
		$info = $data['info'] = $this->rese_model->get_rese_bytype($pid,$type);
		if($info){
			$data['action'] = '/admin/rese/update/' . $type.'/'.$pid;
			$data['button'] = '更新'.$data['title'];
		}
		
		$data['type'] = $type;
		$this->load->view('admin/rese/action.tpl',$data);
	}

	public function update(){
		$type = $this->uri->segment(4);
		$types = array('zf','ps','ss','cf');
		if( !in_array($type,$types)){show_404();}


		$pid = (int)$this->uri->segment(5);
		if(!$pid) show_error('错误的操作');

		$info = $data['info'] = $this->rese_model->get_rese_bytype($pid,$type);
		if(!$info)show_404();

		$p = $this->production_model->get_production_byid($pid);
		if(!$p) show_error('该商品可能已经被删除');
		$data['p'] = $p;
		
		$this->load->library('form_validation');
		$this->form_validation->set_rules('content', '内容', 'trim|required');
		if($this->form_validation->run()){
			$r = $this->rese_model->update($pid,$type);
			if($r['status']){
				$this->session->set_flashdata('success','更新成功！');
			}else{
				$this->session->set_flashdata('error',$r['msg']);
			}
			redirect('/admin/rese/action/' . $type . '/' . $pid);
		}else{
			$this->session->set_flashdata('error',validation_errors());
			redirect('/admin/rese/action/' . $type . '/' . $pid);
		}
	}
}

/* End of file production.php */
/* Location: ./application/controllers/admin/production.php */